package robato.ui;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;

import robato.logic.Robot;

public class FrameMatriz extends JFrame{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -5338911355243342975L;
	String arrSimbolos[];
	String arrSignificados[];
	Robot robots[];
	
	public FrameMatriz(){
		//this.setDefaultCloseOperation(HIDE_ON_CLOSE);
	}
	
	public FrameMatriz(Robot robots[]){
		this.robots = robots;
		this.arrSignificados = this.robots[0].arrMeanings;
		this.arrSimbolos = this.robots[0].arrSymbols;
		//this.setDefaultCloseOperation(HIDE_ON_CLOSE);
	}
	
	public synchronized void setRobots(Robot robots[]){
		this.robots = robots;
		this.arrSignificados = this.robots[0].arrMeanings;
		this.arrSimbolos = this.robots[0].arrSymbols;
	}
	
	
	public synchronized void printMatriz(int indexRobot) {
		
		JPanel panelGeneral = new JPanel();
		JPanel panelRobot;
		JPanel panelColor;
		Robot robot;
		int filas = this.arrSimbolos.length;
		int columnas = this.arrSignificados.length;
		int intLado = (int) Math.ceil(Math.sqrt(this.robots.length));
		
		
		if (indexRobot == -1) {
			this.setTitle("Todos Los Robots");
			panelGeneral.setLayout(new GridLayout(intLado, intLado));
			for (int r = 0; r < this.robots.length; r++) {
				robot = robots[r];
				panelRobot = new JPanel(new GridLayout(filas, columnas));
				panelRobot.setBorder(BorderFactory.createTitledBorder("Matriz " + robot.getName()));
				for (int i = 0; i < filas; i++) {
					for (int j = 0; j < columnas; j++) {
						panelColor = new JPanel();
						panelColor.setBackground(this.getColor((int) (robot.conocimiento[i][j] * 254)));
						panelRobot.add(panelColor);
					}
				}
				panelGeneral.add(panelRobot);
			}
		} else {
			panelGeneral.setLayout(new GridLayout(1, 1));
			robot = this.robots[indexRobot];
			this.setTitle("Matriz Robot: " + robot.getName());
			panelRobot = new JPanel(new GridLayout(filas, columnas));
			panelRobot.setBorder(BorderFactory.createTitledBorder("Matriz " + robot.getName()));
			for (int i = 0; i < filas; i++) {
				for (int j = 0; j < columnas; j++) {
					panelColor = new JPanel();
					panelColor.setBackground(this.getColor((int) (robot.conocimiento[i][j] * 254)));
					panelRobot.add(panelColor);
				}
			}
			panelGeneral.add(panelRobot);
		}
		this.getContentPane().removeAll();
		this.getContentPane().setLayout(new FlowLayout());
		this.setContentPane(panelGeneral);	
		this.validate();
		this.pack();
	}

	private Color getColor(int r) {
		return new Color(254 - r, 254 - r, 254 - r);
	}
	
}
